<template>
  <div class="execution">
    <basic-container>
      <el-card class="filter-container" shadow="never">
        <div style="margin-top: 10px">
          <el-form :inline="true" size="small" label-width="140px">
            <el-form-item label="商品名称">
              <el-input v-model="listQuery.productName"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="getList" type="primary" size="small"
                >查询结果</el-button
              >
              <el-button @click="handleResetSearch" size="small">
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
      <div style="margin-top: 10px">
        <el-table
          :data="list"
          border
          @selection-change="handleSelectionChange"
          :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }"
        >
          <el-table-column
            type="selection"
            width="60"
            align="center"
          ></el-table-column>
          <el-table-column label="商品名称" align="center">
            <template slot-scope="{ row }">
              <img :src="row.productPic" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column
            label="商品名称"
            min-width="120"
            align="center"
            prop="productName"
          >
          </el-table-column>
          <el-table-column
            label="商品规格"
            min-width="120"
            align="center"
            prop="sku"
          >
          </el-table-column>
          <el-table-column
            label="价格"
            align="center"
            prop="price"
          ></el-table-column>
          <el-table-column label="一级用户返点" align="center" prop="levelOne">
            <template slot-scope="{ row }">
              <el-input v-model="row.levelOne"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="二级用户返点" align="center" prop="levelTwo">
            <template slot-scope="{ row }">
              <el-input v-model="row.levelTwo"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="技术收益金额" align="center">
            <template slot-scope="{ row }">
              <el-input v-model="row.fee"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
          ></el-table-column>
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :page-size="listQuery.size"
          :page-sizes="[5, 10, 15]"
          :current-page.sync="listQuery.current"
          :total="total"
        >
        </el-pagination>
      </div>
      <div style="margin-top: 20px">
        <el-button type="primary" @click="handelDetermine">确定</el-button>
      </div>
    </basic-container>
  </div>
</template>
<script>
import "@/styles/index.scss";
import {
  rebateQueryPro,
  setUpProductRebates,
} from "@/api/profit/productRebate/index";

const defaultListQuery = {
  current: 1,
  size: 10,
  productName: null,
};

export default {
  data() {
    return {
      multipleSelection: [],
      list: [],
      total: 0,
      listQuery: Object.assign({}, defaultListQuery),
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, this.defaultListQuery);
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.getList();
    },
    getList() {
      if(this.listQuery.productName == null){
        let prams={
          current: this.listQuery.current,
          size: this.listQuery.size,
        }
        rebateQueryPro(prams).then((res) => {
          this.list = res.data.data.records;
          this.total = res.data.data.total;
        });
      }else{
        rebateQueryPro(this.listQuery).then((res) => {
          this.list = res.data.data.records;
          this.total = res.data.data.total;
        });
      }
      // console.log('this.listQuery===',this.listQuery)

    },
    handelDetermine() {
      this.$confirm("是否要进行新增商品返点操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.multipleSelection.forEach((item) => {
          if (!item.levelOne || !item.levelTwo || !item.fee) {
            this.$message({
              message: "请你检查仔细商品返点是否没填写完整~",
              type: "warning",
              duration: 1000,
            });
            return false;
          }
        });

        let params = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          params.push({
            productId: this.multipleSelection[i].productId,
            skuId: this.multipleSelection[i].skuId,
            levelOne: this.multipleSelection[i].levelOne,
            levelTwo: this.multipleSelection[i].levelTwo,
            fee: this.multipleSelection[i].fee,
          });
        }
        setUpProductRebates({ plist: params }).then((res) => {
          if (res.data.code === 200) {
            this.$message({
              message: res.data.msg,
              type: "success",
              duration: 1000,
            });
            this.$router.go(-1);
          } else {
            this.$message({
              message: res.data.msg,
              type: "danger",
              duration: 1000,
            });
          }
        });
      });
    },
  },
};
</script>
